﻿Imports System.Data
Imports ADOEntityETL
Imports ADOEntity

Partial Class Form_frmCheckCustomer
    Inherits System.Web.UI.Page

    'Private ds As New dsDailyETLSync

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim strdeptCode As String
        Dim strBranchCode As String
        Dim struserName As String
        Dim strETL_STATUS As String
        Dim strStartDate As String
        Dim strEndDate As String
        Dim strCUSTTYPE As String
        'If Not (IsPostBack) Then
        '    hdfVar1.Value = Request.Item("deptCode")
        '    hdfVar2.Value = Request.Item("BranchCode")
        '    hdfVar3.Value = Request.Item("userName")
        '    hdfVar4.Value = Request.Item("ETL_STATUS")
        '    hdfVar5.Value = Request.Item("StartDate")
        '    hdfVar6.Value = Request.Item("EndDate")
        '    hdfVar7.Value = Request.Item("CUSTTYPE")


        'Else
        '    '  ds = Session("ds")
        'End If



        If Not (IsPostBack) Then
            hdfVar1.Value = Request.Item("deptCode")
            hdfVar2.Value = Request.Item("BranchCode")
            hdfVar3.Value = Request.Item("userName")
            hdfVar4.Value = Request.Item("ETL_STATUS")
            hdfVar5.Value = Request.Item("StartDate")
            hdfVar6.Value = Request.Item("EndDate")
            hdfVar7.Value = Request.Item("CUSTTYPE")

            strdeptCode = Request.Item("deptCode")
            strBranchCode = Request.Item("BranchCode")
            struserName = Request.Item("userName")
            strETL_STATUS = Request.Item("ETL_STATUS")
            strStartDate = Request.Item("StartDate")
            strEndDate = Request.Item("EndDate")
            strCUSTTYPE = Request.Item("CUSTTYPE")

        Else
            If Not (IsCallback) Then
                strdeptCode = convertStringEncode(hdfVar1.Value)
                strBranchCode = convertStringEncode(hdfVar2.Value)
                struserName = convertStringEncode(hdfVar3.Value)
                strETL_STATUS = convertStringEncode(hdfVar4.Value)
                strStartDate = convertStringEncode(hdfVar5.Value)
                strEndDate = convertStringEncode(hdfVar6.Value)
                strCUSTTYPE = convertStringEncode(hdfVar7.Value)
            Else
                strdeptCode = hdfVar1.Value
                strBranchCode = hdfVar2.Value
                struserName = hdfVar3.Value
                strETL_STATUS = hdfVar4.Value
                strStartDate = hdfVar5.Value
                strEndDate = hdfVar6.Value
                strCUSTTYPE = hdfVar7.Value
            End If

        End If
        dailyEtl(strdeptCode, strBranchCode, struserName, strETL_STATUS, strStartDate, strEndDate, strCUSTTYPE)

    End Sub

    Private Function convertStringEncode(ByVal strCon As String) As String
        Dim rtStr As String = ""
        Try

            rtStr = Encoding.UTF8.GetString(Encoding.GetEncoding("tis-620").GetBytes(strCon))
        Catch ex As Exception
            Throw ex
        End Try
        Return rtStr
    End Function

     

    Private Sub dailyEtl(ByVal strDeptCode As String, ByVal strBranchCode As String, _
                         ByVal strUserName As String, ByVal strStatus As String, _
                         ByVal strStartDate As String, ByVal strEndDate As String, ByVal strCusType As String)

        Dim clsVarParameter As clsVariable
        Dim clsSessionMenuRight As New clsSessionMenuRight
        Dim clsFormatDate As New clsDateFormat
        Dim colVarParameter As Collection = New Collection
        Dim StrWhere As String = ""
        Dim strConcat As String = " where "
        Dim strPrmCondition As String = ""
        Dim strSql As String = ""
        Dim arrDepCode As String() = Nothing
        Dim arrBranchCode As String() = Nothing
        Dim arrStatus As String() = Nothing


        Dim ds As New dsDailyETLSync
        Dim cls As New clsConnToDbByReport
        Dim re As rptCheckcustomer = New rptCheckcustomer



        clsSessionMenuRight = Session("ClsMenuRight")


        If (clsSessionMenuRight.getRoleGroup > 89) Then

        ElseIf (clsSessionMenuRight.getRoleGroup > 79) Then
            StrWhere = String.Format("{0}{1}ur.ROLECODE < '" & clsSessionMenuRight.getRoleGroup & "'", StrWhere, strConcat)
            strConcat = " And "
        ElseIf (clsSessionMenuRight.getRoleGroup > 69) Then
            StrWhere = String.Format("{0}{1}ur.ROLECODE < '" & clsSessionMenuRight.getRoleGroup & "'", StrWhere, strConcat)
            strConcat = " And "
        ElseIf (clsSessionMenuRight.getRoleGroup > 59) Then
            StrWhere = String.Format("{0}{1}ur.ROLECODE < '" & clsSessionMenuRight.getRoleGroup & "' and ur.ZONECODE='" & clsSessionMenuRight.getZoneCode & "'", StrWhere, strConcat)
            strConcat = " And "

        ElseIf (clsSessionMenuRight.getRoleGroup > 49) Then
            StrWhere = String.Format("{0}{1}ur.ROLECODE < '" & clsSessionMenuRight.getRoleGroup & "'", StrWhere, strConcat)
            strConcat = " And "

        ElseIf (clsSessionMenuRight.getRoleGroup < 50) Then
            StrWhere = String.Format("{0}{1}ur.USERNAME = '" & clsSessionMenuRight.getUserName & "'", StrWhere, strConcat)
            strConcat = " And "
        End If

        If Not strCusType = "" Then
            StrWhere = String.Format("{0}{1}ds.CUSTTYPE = '" & strCusType & "'", StrWhere, strConcat)
            strConcat = " And "

        End If

        If Not strDeptCode = "" Then
            arrDepCode = strDeptCode.Split("/")
            If Not arrDepCode(0) = "0" Then
                StrWhere = String.Format("{0}{1}ds.DEPTCODE = :DEPTCODE", StrWhere, strConcat)
                strConcat = " And "

                clsVarParameter = New clsVariable() With {.nameColumn = "DEPTCODE", _
                                                          .valueColumn = arrDepCode(0)}
                colVarParameter.Add(clsVarParameter)


                re.prmDep.Value = arrDepCode(0) & " : " & arrDepCode(1)
                'If (strPrmCondition = "") Then
                '    strPrmCondition = strPrmCondition & String.Format(Resources.resPrintRptMaster.cdtDept, arrDepCode(1))
                'Else
                '    strPrmCondition = strPrmCondition & "   ;   " & String.Format(Resources.resPrintRptMaster.cdtDept, arrDepCode(1))
                'End If
            Else
                re.prmDep.Value = " - "
                'If (strPrmCondition = "") Then
                '    strPrmCondition = strPrmCondition & String.Format(Resources.resPrintRptMaster.cdtDept, "-")
                'Else
                '    strPrmCondition = strPrmCondition & "   ;   " & String.Format(Resources.resPrintRptMaster.cdtDept, "-")
                'End If
            End If
        End If

        If Not strBranchCode = "" Then
            arrBranchCode = strBranchCode.Split("/")
            If Not arrBranchCode(0) = "0" Then
                StrWhere = String.Format("{0}{1}ds.BRANCHCODE = :BRANCHCODE", _
                                         StrWhere, _
                                         strConcat)
                strConcat = " And "
                clsVarParameter = New clsVariable() With {.nameColumn = "BRANCHCODE", _
                                                          .valueColumn = arrBranchCode(0)}
                colVarParameter.Add(clsVarParameter)
                'If (strPrmCondition = "") Then
                '    strPrmCondition = strPrmCondition & String.Format(Resources.resPrintRptMaster.cdtBranch, arrDepCode(1))
                'Else
                '    strPrmCondition = strPrmCondition & "   ;   " & String.Format(Resources.resPrintRptMaster.cdtBranch, arrDepCode(1))
                'End If
                re.prmBranch.Value = arrBranchCode(0) & " : " & arrBranchCode(1)
            Else
                re.prmBranch.Value = " - "
                'If (strPrmCondition = "") Then
                '    strPrmCondition = strPrmCondition & String.Format(Resources.resPrintRptMaster.cdtBranch, "-")
                'Else
                '    strPrmCondition = strPrmCondition & "   ;   " & String.Format(Resources.resPrintRptMaster.cdtBranch, "-")
                'End If
            End If
        End If


        If Not strUserName = "" Then
            If (strUserName.Length < 5) Then

                StrWhere = String.Format("{0}{1}ur.USERNAME like '%' || :USERNAME || '%'", _
                                              StrWhere, _
                                              strConcat)
            Else

                StrWhere = String.Format("{0}{1}ur.USERNAME = :USERNAME", _
                                                   StrWhere, _
                                                   strConcat)
            End If

            strConcat = " And "
            clsVarParameter = New clsVariable() With {.nameColumn = "USERNAME", _
                                                      .valueColumn = strUserName}
            colVarParameter.Add(clsVarParameter)

            'If (strPrmCondition = "") Then
            '    strPrmCondition = strPrmCondition & String.Format(Resources.resPrintRptMaster.cdtUserName, strUserName)
            'Else
            '    strPrmCondition = strPrmCondition & "   ;   " & String.Format(Resources.resPrintRptMaster.cdtUserName, strUserName)
            'End If
            re.prmUserId.Value = strUserName

        Else
            re.prmUserId.Value = " - "
            'If (strPrmCondition = "") Then
            '    strPrmCondition = strPrmCondition & String.Format(Resources.resPrintRptMaster.cdtUserName, "-")
            'Else
            '    strPrmCondition = strPrmCondition & "   ;   " & String.Format(Resources.resPrintRptMaster.cdtUserName, "-")
            'End If
        End If

        If Not strStatus = "" Then
            arrStatus = strStatus.Split("/")

            If Not arrStatus(0) = "0" Then
                StrWhere = String.Format("{0}{1}ds.ETL_STATUS =  :ETL_STATUS ", _
                                    StrWhere, _
                                    strConcat)
                strConcat = " And "

                clsVarParameter = New clsVariable() With {.nameColumn = "ETL_STATUS", _
                                                          .valueColumn = arrStatus(0)}
                colVarParameter.Add(clsVarParameter)

                re.prmETL_STATUS.Value = arrStatus(0) & " : " & arrStatus(1)
                'If (strPrmCondition = "") Then
                '    strPrmCondition = strPrmCondition & String.Format(Resources.resPrintRptMaster.cdtETL_STATUS, arrStatus(1))
                'Else
                '    strPrmCondition = strPrmCondition & "   ;   " & String.Format(Resources.resPrintRptMaster.cdtETL_STATUS, arrStatus(1))
                'End If
            Else
                re.prmETL_STATUS.Value = " - "
                'If (strPrmCondition = "") Then
                '    strPrmCondition = strPrmCondition & String.Format(Resources.resPrintRptMaster.cdtETL_STATUS, "-")
                'Else
                '    strPrmCondition = strPrmCondition & "   ;   " & String.Format(Resources.resPrintRptMaster.cdtETL_STATUS, "-")
                'End If

            End If

        End If

        If Not (strStartDate = "" And strEndDate = "") Then
            Dim dteStartDate As Date = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, strStartDate) & " 00:00:00"
            Dim dteEndDate As Date = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, strEndDate) & " 23:59:59"
            StrWhere = String.Format("{0}{1}ds.ETL_UPDATEDATE >= :StartDate and ds.ETL_UPDATEDATE <= :EndDate", _
                                     StrWhere, _
                                     strConcat)
            strConcat = " And "
            clsVarParameter = New clsVariable() With {.nameColumn = "StartDate", _
                                                      .valueColumn = dteStartDate}
            colVarParameter.Add(clsVarParameter)

            clsVarParameter = New clsVariable() With {.nameColumn = "EndDate", _
                                                      .valueColumn = dteEndDate}
            colVarParameter.Add(clsVarParameter)
            'If (strPrmCondition = "") Then
            '    strPrmCondition = strPrmCondition & String.Format(Resources.resPrintRptMaster.cdtDate, dteStartDate & "   ถึง   " & dteEndDate)
            'Else
            '    strPrmCondition = strPrmCondition & "   ;   " & String.Format(Resources.resPrintRptMaster.cdtDate, dteStartDate & "   ถึง   " & dteEndDate)
            'End If
            re.prmStartDate.Value = dteStartDate
            re.prmEndDate.Value = dteEndDate
        Else
            re.prmStartDate.Value = " - "
            re.prmEndDate.Value = " - "
            'If (strPrmCondition = "") Then
            '    strPrmCondition = strPrmCondition & String.Format(Resources.resPrintRptMaster.cdtDate, "-")
            'Else
            '    strPrmCondition = strPrmCondition & "   ;   " & String.Format(Resources.resPrintRptMaster.cdtDate, "-")
            'End If
        End If



        strSql = "select  ds.Custcode,ds.UNIQUEID,ds.THFIRSTNAME, ds.THMIDNAME, ds.THLASTNAME, " _
                & "        ds.ENFIRSTNAME, ds.ENMIDNAME, ds.ENLASTNAME, ds.CREATEBY, ds.CREATEDATE, " _
                & "        ds.UPDATEBY, ds.DEPD,ds.DEPL,ds.DEPP,ds.UPDATEDATE, rt.ETL_STATUS, " _
                & "        rt.ETL_STATUSDESC, ds.DEPTCODE, ds.ZONECODE, ds.BRANCHCODE, ds.TRANTYPE,ur.ROLECODE " _
                & " from " & System.Configuration.ConfigurationSettings.AppSettings("TableMCUSTOMER") & "  ds  join " & System.Configuration.ConfigurationSettings.AppSettings("Tablemetlstatus") & " rt " _
                & "			on ds.etl_status = rt.etl_status " _
                & "			join " & System.Configuration.ConfigurationSettings.AppSettings("TableSUSER") & " ur on ds.UPDATEBY = ur.USERNAME " _
                & StrWhere _
                & " order by  ds.DEPTCODE asc, ds.BRANCHCODE asc, ds.UPDATEBY asc, " _
                & "   rt.ETL_STATUS asc, ds.UPDATEDATE asc  "



        cls.rptCheckCustomer(re, strSql, ds, colVarParameter)
        ' re.prmWhere.Value = strPrmCondition
        ReportViewer1.Report = re

    End Sub

    'Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
    '    Session("ds") = ds
    'End Sub
End Class
